//
//  MainViewController.swift
//  新浪微博
//
//  Created by apple on 15/7/28.
//  Copyright © 2015年 Adam. All rights reserved.
//

import UIKit

class MainViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()

       addChildViewControllers()
        setupComposeButton()
    }
    
    private func addChildViewControllers() {
    addChildViewController(HomeTableViewController(), title: "首页", image: "tabbar_home")
    addChildViewController(MessageTableViewController(), title: "消息", image: "tabbar_message_center")
        //这里添加一个UIViewController来占据位置
        addChildViewController(UIViewController())
        addChildViewController(DiscoverTableViewController(), title: "发现", image: "tabbar_discover")
        addChildViewController(ProfileTableViewController(), title: "我", image: "tabbar_profile")
    }
 
    private func setupComposeButton(){
     let w=tabBar.bounds.width/CGFloat(childViewControllers.count)
     let rect = CGRect(x: 0, y: 0, width: w, height: tabBar.bounds.height)
        //下面用到懒加载
        composedButton.frame=CGRectOffset(rect, 2*w, 0)
    }
    private func addChildViewController(vc:UIViewController,title:String,image:String) {
        vc.title=title
        vc.tabBarItem.image=UIImage(named: image)
        let nav = UINavigationController(rootViewController: vc)
        addChildViewController(nav)
    }
    private lazy var composedButton:UIButton={
      let btn=UIButton()
        btn.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        btn.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState:UIControlState.Highlighted)
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        //懒加载中需要使用self.
        self.tabBar.addSubview(btn)
        btn.addTarget(self, action: "clickComposeButton", forControlEvents: UIControlEvents.TouchUpInside)
        return btn
    }()
}
